package com.shuijianbaozi.child.testdemo;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hitingaa.child.oversealib.fplay.device.BoxMannger;
import com.hitingaa.child.oversealib.fplay.device.FplayDevice;
import com.hitingaa.child.oversealib.fplay.device.FplayDeviceMng;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.qqtheme.framework.picker.TimePicker;
import cn.qqtheme.framework.util.ConvertUtils;

/**
 * 用于测试
 *
 * @author shuijianbaozi
 */
public class MainActivity extends Activity {
    public static String TAG = "MainActivity";

    @InjectView(R.id.btnInitNet)
    Button btnInitNet;
    @InjectView(R.id.btnGetdves)
    Button btnGetdves;
    @InjectView(R.id.btnGroup)
    Button btnGroup;
    @InjectView(R.id.btnReleaseGroup)
    Button btnReleaseGroup;
    @InjectView(R.id.tv_tip)
    TextView tvTip;
    @InjectView(R.id.tv_title)
    TextView tvTitle;
    @InjectView(R.id.btn_timeSelect)
    Button btnTimeSelect;
    @InjectView(R.id.btnTimestamp)
    Button btnTimestamp;
    @InjectView(R.id.activity_main)
    RelativeLayout activityMain;
    @InjectView(R.id.btnString2color)
    Button btnString2color;
    @InjectView(R.id.etInput)
    EditText etInput;
    @InjectView(R.id.btn_test_Constraintlayout)
    Button btnTestConstraintlayout;
    @InjectView(R.id.btnViewPager)
    Button btnViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);
        tvTip.setMovementMethod(new ScrollingMovementMethod());
        tvTip.setSelected(true);

        tvTitle.setText(getResources().getText(R.string.tip));
    }


    @OnClick({R.id.btnInitNet, R.id.btnGetdves, R.id.btnGroup, R.id.btnReleaseGroup, R.id.btn_timeSelect, R.id.btnTimestamp
            , R.id.btnString2color, R.id.btn_test_Constraintlayout,R.id.btnViewPager})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btnInitNet:
                FplayDeviceMng.getInstance().init(this);
                break;
            case R.id.btnGetdves:
                List<FplayDevice> devList = BoxMannger.getInstance(this).getDevList();
                Log.d(TAG, "testNetWorking: " + devList.toString());
                break;
            case R.id.btnGroup:
                break;
            case R.id.btnReleaseGroup:
                break;
            case R.id.btn_timeSelect://显示时间选择器
                TimePicker picker = new TimePicker(this, TimePicker.HOUR_24);
                picker.setUseWeight(false);
                picker.setCycleDisable(true);
                picker.setRangeStart(0, 0);//00:00
                picker.setRangeEnd(23, 59);//23:59
                int currentHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
                int currentMinute = Calendar.getInstance().get(Calendar.MINUTE);
                picker.setSelectedItem(currentHour, currentMinute);
                picker.setTopLineVisible(false);
                picker.setTextPadding(ConvertUtils.toPx(this, 15));
                picker.setOnTimePickListener(new TimePicker.OnTimePickListener() {
                    @Override
                    public void onTimePicked(String hour, String minute) {
                        showToast(hour + ":" + minute);
                    }
                });
                picker.show();
                break;
            case R.id.btnTimestamp://时间毫秒转化字符串
                long ms = 7194000;//09:59:54 01:59:54   如果是计算机的时间戳需要设置时区
                ms = System.currentTimeMillis();//获取当前当地时间的时间戳
                SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
                //formatter.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));//必须加,否则默认是零时区时间
                String hms = formatter.format(ms);
                Log.d(TAG, "onViewClicked: hms " + hms);
                showToast("当前时间戳转化为时间 " + hms);
                break;
            case R.id.btnString2color:
                String inputText = etInput.getText().toString();
                if (inputText != null) {
                    try {
                        Log.d(TAG, "onViewClicked: inputText is " + inputText);
                        int color = Color.parseColor(inputText);
                        showToast("输入框: " + color);
                        btnString2color.setBackgroundColor(color);
                    } catch (Exception e) {
                        e.printStackTrace();
                        showToast(e.getMessage());
                    }
                }

                break;
            case R.id.btn_test_Constraintlayout:
                showToast("跳转到Constraintlayout测试界面");
                Intent intent = new Intent(this, ConstraintActivity.class);
                startActivity(intent);
                break;
            case R.id.btnViewPager:
                showToast("viewpager 测试");
                startActivity(new Intent(this,Main2Activity.class));
                break;
        }
    }

    private void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

}
